﻿//Write a program that reads a rectangular matrix of size N x M and finds in it the square 3 x 3 that has maximal sum of its elements.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02MaxPlatform
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter width of matrix M:");
            int m = int.Parse(Console.ReadLine());
            Console.Write("Enter depth of matrix N:");
            int n = int.Parse(Console.ReadLine());
            int[,] matrix = new int[n,m];
            //Populating some values:************************************************************
            int value = 1;
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    matrix[i, j] = value;
                    value++;
                }
            }
            //Printing the matrix:***************************************************************
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,2} ",matrix[i,j]);
                }
                Console.WriteLine();
            }
            //Going through the matrix with the imaginary platform:******************************
            int bestSum = int.MinValue;
            int row = 0;
            int col = 0;
            int sum = 0;
            for (int i = 0; i < matrix.GetLength(0)-2; i++)
            {
                for (int j = 0; j < matrix.GetLength(1)-2; j++)
                {
                    sum = matrix[i, j] + matrix[i, j + 1] + matrix[i, j + 2] +
                            matrix[i + 1, j] + matrix[i + 1, j + 1] + matrix[i + 1, j + 2] +
                            matrix[i + 2, j] + matrix[i + 2, j + 1] + matrix[i + 2, j + 2];
                    if (sum > bestSum)
                    {
                        bestSum = sum;
                        row = i;
                        col = j;
                    }
                }
            }
            //Printing the result:
            Console.WriteLine("The best paltform is:");
            Console.Write("{0,2} {1,2} {2,2} \r\n{3,2} {4,2} {5,2} \r\n{6,2} {7,2} {8,2} ",
                            matrix[row, col], matrix[row, col + 1], matrix[row, col + 2],
                            matrix[row + 1, col], matrix[row + 1,col + 1], matrix[row + 1, col + 2],
                            matrix[row + 2, col], matrix[row + 2, col + 1], matrix[row + 2, col + 2]);

        }
    }
}
